import 'dart:async';
import 'package:acaleph/utils/http_util.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';


class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List items = [{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"},{"title":"1"}];
  ScrollController _scrollController = new ScrollController();
  bool isPerformingRequest = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getMoreData();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  _getMoreData() async {
    if (!isPerformingRequest) {
      setState(() => isPerformingRequest = true);
      List newEntries = await fakeRequest(
          items.length, items.length + 10); //returns empty list
      if (newEntries.isEmpty) {
        double edge = 50.0;
        double offsetFromBottom = _scrollController.position.maxScrollExtent -
            _scrollController.position.pixels;
        if (offsetFromBottom < edge) {
          _scrollController.animateTo(
              _scrollController.offset - (edge - offsetFromBottom),
              duration: new Duration(milliseconds: 500),
              curve: Curves.easeOut);
        }
      }
      setState(() {
        items.addAll(newEntries);
        isPerformingRequest = false;
      });
    }
  }


  Future<List> fakeRequest(int from, int to) async {


    int arg1 = (from/(to - from)+1).toInt();
    int arg2 = (to-from).toInt();
    Response response;
    response=await new HttpUtil().post("/ipArticle/list",data:{"pageNum":arg1,"pageSize":arg2,"type":2,"sortColumn":'add_time'});
    print(response.data.toString());
//    print(response.data["data"].toString());

    var temp = response.data["data"]["list"];
    print(temp.toString());


    return temp;
//    return Future.delayed(Duration(seconds: 0.3), () {
//      return temp;
//    });
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Center(
        child: new Opacity(
          opacity: isPerformingRequest ? 1.0 : 0.0,
          child: new CircularProgressIndicator(),
        ),
      ),
    );
  }

  @override
  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: AppBar(
        title: Text("Infinite ListView"),
      ),
      body: ListView.builder(
        itemCount: items.length + 1,
        itemBuilder: (context, index) {
          if (index == items.length) {
            return _buildProgressIndicator();
          } else {

            return ListTile(title: new Text(items[index]["title"]));
//            return ListTile(title: new Text("Number $index"));
          }
        },
        controller: _scrollController,
      ),
    );
  }
}
